package com.study.thymeleaf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.study.thymeleaf.core.R;
import com.study.thymeleaf.entity.Dept;
import com.study.thymeleaf.service.DeptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    DeptService deptService;

    /**
     * 新增
     * @param dept
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/dept")
    public R<Object> save(@RequestBody Dept dept) {
        deptService.save(dept);
        return R.ok(dept);
    }

    /**
     * 根据主键删除
     * @param deptno
     * @return
     */
    @ApiOperation("根据主键删除")
    @DeleteMapping("/dept/{deptno}")
    public R<Object> removeById(@PathVariable("deptno") Integer deptno) {
        deptService.removeById(deptno);
        return R.ok().setMsg("删除成功");
    }

    /**
     * 批量删除(物理删除)
     * @param deptnos
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("/removeBatch")
    public R<Object> removeBatch(@RequestBody List<Integer> deptnos) {
        deptService.removeBatchByIds(deptnos);
        return R.ok().setMsg("删除成功");
    }

    /**
     * 根据主键更新
     * @param dept
     * @return
     */
    @ApiOperation("根据主键更新")
    @PutMapping("/dept")
    public R<Object> updateById(@RequestBody Dept dept) {
        deptService.updateById(dept);
        return R.ok().setMsg("更新成功");
    }

    /**
     * 根据主键查找
     * @param deptno 主键
     * @return
     */
    @ApiOperation("根据主键查找")
    @GetMapping("/dept/{deptno}")
    public R<Dept> getById(@PathVariable("deptno") Integer deptno) {
        Dept dept = deptService.getById(deptno);
        return R.ok(dept);
    }

    @ApiOperation("分页查询")
    @PostMapping("/getDeptListByPage")
    public R<IPage<Dept>> selectList(@RequestBody Dept param) {
        return R.ok(deptService.page(param.parse(), new QueryWrapper<>(param)));
    }

}
